/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 后台线程工厂类
 * @Description *
 * @Package concurrent.mthread.daemon.factory
 * @Since 2021/7/27 1:46 下午
 */
package concurrent.mthread.daemon.factory;

import java.util.concurrent.ThreadFactory;

public class DaemonThreadFactory implements ThreadFactory {

    /**
     * 线程优先级
     *
     * @author chentl
     * @date 2021/7/28 4:31 下午
     **/
    private int priority;

    /**
     * 构造
     *
     * @param priority 优先级
     */
    public DaemonThreadFactory(int priority) {
        this.priority = priority;
    }

    /**
     * 无参构造
     */
    public DaemonThreadFactory() {
    }

    /**
     * 这个工厂类创建的线程 都会经过下面的包装
     *
     * @param r 线程任务
     * @return {@link Thread}
     * @author chentl
     * @since 1:49 下午 2021/7/27
     **/
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(true);
        t.setPriority(priority);
        return t;
    }
}
